package logic

import (
	"context"
	"github.com/google/uuid"
	"github.com/zeromicro/go-zero/core/logx"
	"goodsrpc/goods"
	"goodsrpc/internal/svc"
	"goodsrpc/mysql"
	"goodsrpc/pkg"
	"strconv"
)

type OrderAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewOrderAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *OrderAddLogic {
	return &OrderAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *OrderAddLogic) OrderAdd(in *goods.OrderAddRequest) (*goods.OrderAddResponse, error) {
	// todo: add your logic here and delete this line
	var h mysql.House
	mysql.DB.Where("id =? ", in.HouseId).Find(&h)
	OrderNo := uuid.NewString()
	PayURL := pkg.Alipay(OrderNo, strconv.Itoa(int(h.Price)))
	order := mysql.Order{
		OrderNo:  OrderNo,
		HouseId:  in.HouseId,
		UserId:   in.UserId,
		Quantity: in.Quantity,
		Price:    h.Price,
		State:    0,
	}
	mysql.DB.Create(&order)

	return &goods.OrderAddResponse{
		PayURL: PayURL,
	}, nil
}
